﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameBase;

namespace GalaxyPresident
{
    public class Team : GameBase.InvisibleGameEntity
    {
        private List<Planet> planets = new List<Planet>();
        //private List<Unit> units = new List<Unit>();
        private List<MotherShip> motherShips = new List<MotherShip>();

        internal List<MotherShip> MotherShips
        {
            get { return motherShips; }
            set { motherShips = value; }
        }
        private Color _teamColor;

        private float totalMoney;
        private float totalPopulation;

       /* public List<Unit> Units
        {
            get { return units; }
            set { units = value; }
        }*/

        public List<Planet> Planets
        {
            get { return planets; }
            set { planets = value; }
        }

        public float TotalMoney
        {
            get { return totalMoney; }
            set { totalMoney = value; }
        }

        public float TotalPopulation
        {
            get { return totalPopulation; }
            set { totalPopulation = value; }
        }

        public Color TeamColor
        {
            get { return _teamColor; }
            set { _teamColor = value; }
        }
        public int GetNumberOfMotherShip(Planet planet)
        {
            int count = 0;
            foreach (MotherShip motherShip in motherShips)
            {
                if (motherShip.PlanetOwner == planet)
                {
                    count++;
                }
            }
            return count;
        }
        public MotherShip GetMotherShip(Planet planet)
        {
            foreach (MotherShip motherShip in motherShips)
            {
                if (motherShip.PlanetOwner == planet)
                {
                    return motherShip;
                }
                
            }
            return null;
        }
        public override void UpdateInfo(GameTime gameTime)
        {
            _currentTotalTime += gameTime.ElapsedGameTime.Milliseconds;
            if (_currentTotalTime > 250)
            {
                _currentTotalTime = 0;
                float amount = 0;
                foreach (Planet pla in planets)
                {
                    amount += pla.getIncreaseMoneyAmount();
                }
                TotalMoney += amount;
            }
            /*foreach (Unit unit in units)
            {
                unit.UpdateInfo(gameTime);
            }*/
            foreach (MotherShip motherShip in motherShips)
            {
                motherShip.UpdateInfo(gameTime);
            }

        }

        public List<Unit> GetUnitsFromPlanet(List<int> number,Planet planet)
        {
            List<Unit> unitsList = new List<Unit>();
            if (planet.TeamOwner != this)
            {
                return unitsList;
            }
            foreach (Unit unit in planet.Units)
            {
                if (unit.GetType() == typeof(SpaceCraft))
                {
                    if (number[0] > 0)
                    {
                        number[0]--;
                        unitsList.Add(unit);
                    }
                }
                if (unit.GetType() == typeof(AirCraft))
                {
                    if (number[1] > 0)
                    {
                        number[1]--;
                        unitsList.Add(unit);
                    }
                }
                if (unit.GetType() == typeof(BomberCraft))
                {
                    if (number[2] > 0)
                    {
                        number[2]--;
                        unitsList.Add(unit);
                    }
                }                
            }
            return unitsList;
        }

        public bool isHaveMotherShipAtPlanet(Planet planetz)
        {
            foreach (MotherShip mothership in motherShips)
            {
                if (mothership.PlanetOwner == planetz)
                    return true;
            }
            return false;
        }

        public List<MotherShip> getAllMotherShipAtPlanet(Planet planetz)
        {
            List<MotherShip> result = new List<MotherShip>();

            foreach (MotherShip mothership in motherShips)
            {
                if (mothership.PlanetOwner == planetz)
                    result.Add(mothership);
            }

            return result;
        }

        public MotherShip getMotherShipHaveEnoughResourceForBuildCapitalAtPlanet(Planet planetz)
        {
            List<MotherShip> result = getAllMotherShipAtPlanet(planetz);
            foreach (MotherShip mothershipzzz in result)
                if (mothershipzzz.isEnoughResourceForBuildCapital())
                    return mothershipzzz;
            return null;
        }

        public bool isHaveMotherShipEnoughResourceForBuildCapitalAtPlanet(Planet planetz)
        {
            return getMotherShipHaveEnoughResourceForBuildCapitalAtPlanet(planetz) != null;
        }

        public List<MotherShip> getAllMotherShipAreWorking()
        {
            List<MotherShip> result = new List<MotherShip>();

            foreach (MotherShip motherShipzzz in MotherShips)
                if (motherShipzzz.PlanetOwner != null)
                    if (motherShipzzz.PlanetOwner.TeamOwner != motherShipzzz.TeamOwner)
                        result.Add(motherShipzzz);

            return result;
        }
    }
}
